#!/bin/bash

# this script kills the flask gunicorn service 

while getopts ":s:" opt; do
    case $opt in
        s)
            echo "-s was triggered, Parameter: $OPTARG" >&2
            application=$OPTARG;
            application_pid_file="gunicorn_pid_$OPTARG";

            if [ $application == "app1" ]; then  
                while read line;
                    do mypid=$line; done < $application_pid_file
                    # do echo "$line"; done < gunicorn_master

                echo $mypid; # make sure we have the right pid
                kill $mypid; # okay now kill the script
            elif [ $application == "login" ]; then  
                while read line;
                    do mypid=$line; done < $application_pid_file
                    # do echo "$line"; done < gunicorn_master

                echo $mypid; # make sure we have the right pid
                kill $mypid; # okay now kill the script
            else
                echo "this is not a recognized application"
                exit 1
            fi
            ;;
        \?)
            echo "Invalid option: $OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires and argument." >&2
            exit 1
            ;;
    esac
done
 
